<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/page/common/taglibs.jsp" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<input type="button" value="添加" onclick="insert()"/>&nbsp;<br/>
<input type="button" value="录入" onclick="lu()"/>&nbsp;

<table cellspacing="0" cellpadding="20" border="1px" align="center" >
    <tr>
        <th><input type="checkbox" onclick="fanx_onclick()"/></th>
        <th>编号</th>
        <th>考场名称</th>
        <th>参考人数</th>
        <th>上限</th>
        <th>考试状态</th>
        <th>操作</th>
    </tr>
    <tbody id="td">

    </tbody>
</table>
</body>
<script type="text/javascript">
    $(function(){
        search();
    });
    
    function search(){
        $.post(
            "${ctx}/exam/show",
            {},
            function (result){
                html = "";
                for (let i = 0; i < result.data.length; i++) {
                    var sco = result.data[i];
                    html +="<tr>";
                    html += "<td><input type='checkbox' class='check' name='ids' value='"+sco.id+"'/></td>";
                    html += "<input type='hidden' id='"+sco.id+"' value='"+sco.id+"'/>";
                    html +="<td>"+sco.id+"</td>";
                    html +="<td>"+sco.examName+"</td>";
                    html +="<td>"+sco.nend+"</td>";
                    html +="<td>"+sco.upper+"</td>";
                    html +="<td>"+sco.status+"</td>";
                    html +="<td>";
                    html +="<input type='button' value='详情' onclick='cha("+sco.id+")'/>";
                    html +="</td>";
                    html +="</tr>";
                }
                $("#td").html(html);
            }
        );
    }

    //全选
    function fanx_onclick() {
        //反选 首先要有所有对象
        var quanx = document.getElementsByClassName("check");
        //对象是多个，需要遍历去找到所有的值
        for (var i = 0; i < quanx.length; i++) {
            //判断它选择的值
            if (quanx[i].checked == true) {
                quanx[i].checked = false;
            } else {
                quanx[i].checked = true;
            }
        }
    }

    function insert(){
        layer.open({
            type: 2,
            area: ['500px', '300px'],
            content:"${ctx}/exam/toInsert"
        });
    }

    function lu(){
        var chk_value=[];
        $('input[name="ids"]:checked').each(function(){
            chk_value.push($(this).val());
        });
        if(chk_value.length==0){
            layer.msg("至少选择一项");
            return;
        }
        if(chk_value.length>1){
            layer.msg("只能选择一项");
            return;
        }
        layer.open({
            title:'新增信息',
            type:2,
            content:'${ctx}/exam/toInsertUser/'+chk_value,
            area: ['500px', '300px']
        });
    }

    function cha(id){
        layer.open({
            title:'查询信息',
            type:2,
            content:'${ctx}/exam/toQueryList/'+id,
            area: ['500px', '300px']
        });
    }
</script>
</html>